// distributor/pages/myTeam/myTeam.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        chooseClass: 1,
        page: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.hideShareMenu();
        let that = this;
        if (options.id) {
            that.setData({
                level_id: options.id
            });
        }
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.setData({
            page: 1,
            gruopList: []
        });
        that.getGroupList();
        this.getBase();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getGroupList();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    var copyText = [];
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                } else {
                    copyText = "";
                }
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    title: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    lock_fans: res.extra.lock_fans,
                    share_link: res.extra.share_link,
                    share_type: res.extra.share_type,
                    wxapp_img: res.extra.wxapp_img,
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2,
                    custom_title: res.extra.level_set.custom_title,
                    level_set: res.extra.level_set
                });
                that.getGroupLevel();
            }
        });
    },
    chooseClass: function(e) {
        let that = this;
        that.setData({
            chooseClass: e.currentTarget.dataset.id,
            page: 1,
            gruopList: []
        });
        that.getGroupList();
    },
    getGroupLevel: function() {
        let that = this;
        common.request({
            endpoint: "Level/level_user/group_level",
            method: "post",
            data: {
                level_id: that.data.level_id
            },
            success: res => {
                let groupLevel = res.extra;
                if (groupLevel.length) {
                    groupLevel.map(x => {
                        if (x.keyword == "first") {
                            x.name = that.data.level_set.first_level_name;
                        } else if (x.keyword == "second") {
                            x.name = that.data.level_set.second_level_name;
                        } else if (x.keyword == "third") {
                            x.name = that.data.level_set.third_level_name;
                        }
                    });
                    that.setData({
                        groupLevel: groupLevel,
                        chooseClass: res.extra[0].keyword
                    });
                    that.getGroupList();
                } else {
                    that.setData({
                        gruopList: [],
                        groupLevel: []
                    });
                }
            }
        });
    },
    getGroupList: function() {
        let that = this;
        common.request({
            endpoint: "Level/level_user/group",
            method: "post",
            data: {
                type: that.data.chooseClass,
                level_id: that.data.level_id,
                page: that.data.page
            },
            success: res => {
                var gruopList = res.extra;
                if (gruopList.length) {
                    gruopList = that.data.page == 1 ? gruopList : that.data.gruopList.concat(gruopList);
                    that.setData({
                        gruopList: gruopList,
                        page: that.data.page + 1
                    });
                }
            }
        });
    }
});